package com.itheima.web.manager;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.itheima.domain.db.UserInfo;
import com.itheima.domain.mongo.Comment;
import com.itheima.domain.mongo.Movement;
import com.itheima.domain.mongo.Video;
import com.itheima.domain.vo.CommentVo;
import com.itheima.domain.vo.MovementVo;
import com.itheima.domain.vo.PageBeanVo;
import com.itheima.domain.vo.VideoVo;
import com.itheima.service.mongo.CommentService;
import com.itheima.service.mongo.MovementService;
import com.itheima.service.mongo.VideoService;
import com.itheima.util.DateFormatUtil;
import com.itheimma.service.db.UserInfoService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Component
public class UserManager {

    @DubboReference
    private MovementService movementService;

    @DubboReference
    private UserInfoService userInfoService;

    @DubboReference
    private CommentService commentService;

    @DubboReference
    private VideoService videoService;

    //返回用户列表
    public ResponseEntity findUserList(Integer pageNum, Integer pageSize) {
        PageBeanVo pageBeanVo = userInfoService.findByPage(pageNum, pageSize);
        return ResponseEntity.ok(pageBeanVo);
    }

    //返回用户详情
    public ResponseEntity findUserInfo(Long userId) {
        UserInfo userInfo = userInfoService.findById(userId);
        return ResponseEntity.ok(userInfo);
    }

    //查询动态
    public ResponseEntity findUserMovementList(Integer pageNum, Integer pageSize, Long userId, String state) {

        //获取pbv对象
        PageBeanVo pageBeanVo = movementService.findMyMovementByPage(userId, state, pageNum, pageSize);
        List<Movement> movementList = (List<Movement>) pageBeanVo.getItems();

        ArrayList<MovementVo> vos = new ArrayList<>();
        UserInfo userInfo = userInfoService.findById(userId);
        if (CollectionUtil.isNotEmpty(movementList)) {
            for (Movement movement : movementList) {
                MovementVo vo = new MovementVo();
                vo.setUserInfo(userInfo);
                vo.setMovement(movement);
                vo.setCreateDate(DateUtil.formatDateTime(new Date(movement.getCreated())));   //把时间换一个格式
                vos.add(vo);
            }
        }
        pageBeanVo.setItems(vos);

        return ResponseEntity.ok(pageBeanVo);
    }

    //用户详情
    public ResponseEntity findMovementVoById(String publishId) {
        Movement movement = movementService.findById(publishId);

        MovementVo movementVo = new MovementVo();

        UserInfo userInfo = userInfoService.findById(movement.getUserId());
        movementVo.setUserInfo(userInfo);
        movementVo.setMovement(movement);
        movementVo.setCreateDate(DateUtil.formatDateTime(new Date(movement.getCreated())));

        return ResponseEntity.ok(movementVo);
    }

    //查询评论视频
    public ResponseEntity findCommentVoByPage(String publishId, Integer pageNum, Integer pageSize) {
        PageBeanVo pageBeanVo = commentService.findCommentByPage(publishId, pageNum, pageSize);
        List<Comment> commentList = (List<Comment>) pageBeanVo.getItems();

        ArrayList<CommentVo> vos = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(commentList)) {
            for (Comment comment : commentList) {
                CommentVo commentVo = new CommentVo();
                //评论人信息
                UserInfo userInfo = userInfoService.findById(comment.getUserId());
                commentVo.setId(comment.getId().toHexString());
                commentVo.setAvatar(userInfo.getAvatar());
                commentVo.setNickname(userInfo.getNickname());
                commentVo.setContent(comment.getContent());
                commentVo.setCreateDate(DateUtil.formatDateTime(new Date(comment.getCreated())));

                vos.add(commentVo);
            }
        }
        pageBeanVo.setItems(vos);
        return ResponseEntity.ok(pageBeanVo);
    }

    //视频详情 videoVo =video + userInfo
    public ResponseEntity findVideoVoByPage(Long userId, Integer pageNum, Integer pageSize) {
        PageBeanVo pageBeanVo = videoService.findVideoList(userId, pageNum, pageSize);

        List<Video> videoList = (List<Video>) pageBeanVo.getItems();
        ArrayList<VideoVo> vos = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(videoList)) {
            for (Video video : videoList) {
                VideoVo vo = new VideoVo();
                UserInfo userInfo = userInfoService.findById(video.getUserId());
                vo.setUserInfo(userInfo);
                vo.setVideo(video);
                vos.add(vo);
            }
        }
        pageBeanVo.setItems(vos);

        return ResponseEntity.ok(pageBeanVo);

    }
}
